'use strict'

angular.module('mainApp')
.controller('releaseCtrl',function($scope, tjzHttp, constant, ngDialog ,Upload){
    $scope.jobTypes = [{
        value: 1,
        name: '日常保洁'
    },{
        value: 2,
        name: '保姆月嫂'
    },{
        value: 3,
        name: '物品维修'
    },{
        value: 4,
        name: '货运搬家'
    }];
    $scope.release = function(file){
        var data = {
            jobType: $scope.type,
            title:$scope.title,
            price: $scope.price,
            needNum: $scope.needNum,
            isHot: $scope.isHot,
            salary: $scope.salary,
            info: $scope.info,
        }
        var url = constant.url + '/create/job';
        tjzHttp.postRequest(url,data).then(function(res) {
            if (res.data.code == 0) {
                console.log(res.data);
                Upload.upload({
                    url: constant.url + '/job/file',
                    data: {
                        file: file,
                        jobId: res.data.data.jobId
                    },
                    method: 'POST'
                }).then(function (resp) {
                    if(resp.data.code == 0){
                        ngDialog.open({
                            template: 'template/dialog.html',
                            width: 600,
                            controller: ['$scope',function($scope){
                                $scope.dialogTitle = '淘家政提示';
                                $scope.dialogContent = '发布职位成功';
                                $scope.yes = function(){
                                    $scope.closeThisDialog();
                                }
                            }]
                        });
                    }
                }, function (resp) {

                }, function (evt) {
                    $scope.progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
                    $scope.progressPercentageNum = $scope.progressPercentage / 100;
                    console.log('progress: ' + $scope.progressPercentage + '% ' + evt.config.data.file.name + $scope.progressPercentageNum);
                });
            }else{
                var message = res.data.message;
                ngDialog.open({
                    template: 'template/dialog.html',
                    width: 600,
                    data: {
                        message: message
                    },
                    controller: ['$scope',function($scope){
                        var message = $scope.ngDialogData.message;
                        $scope.dialogTitle = '淘家政提示';
                        $scope.dialogContent = message;
                        $scope.yes = function(){
                            $scope.closeThisDialog();
                        }
                    }]
                });
            }
        })
    }
})
.controller('releasePersonCtrl',function($scope, tjzHttp, constant, ngDialog){
    $scope.jobTypes = [{
        value: 1,
        name: '日常保洁'
    },{
        value: 2,
        name: '保姆月嫂'
    },{
        value: 3,
        name: '物品维修'
    },{
        value: 4,
        name: '货运搬家'
    }];
    $scope.releasePerson = function(){
        var data = {
            userType: $scope.type,
            info: $scope.info
        }
        tjzHttp.postRequest(constant.url + '/create/user', data).then(function(res){
            if(res.data.code == 0){
                ngDialog.open({
                    template: 'template/dialog.html',
                    width: 600,
                    controller: ['$scope',function($scope){
                        $scope.dialogTitle = '淘家政提示';
                        $scope.dialogContent = '发布成功';
                        $scope.yes = function(){
                            $scope.closeThisDialog();
                        }
                    }]
                });
            }else{
                var message = res.data.message;
                ngDialog.open({
                    template: 'template/dialog.html',
                    width: 600,
                    data: {
                        message: message
                    },
                    controller: ['$scope','ngDialog',function($scope,ngDialog){
                        $scope.dialogTitle = '淘家政提示';
                        $scope.dialogContent = $scope.ngDialogData.message;
                        $scope.yes = function(){
                            $scope.closeThisDialog();
                        }
                    }]
                });
            }
        })
    }
})